package com.company.ui;

import com.company.bean.Employee;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class EmployeeManagementFrame extends JFrame {
    private JTextField searchField; // 搜索框
    private JButton searchButton, addButton; // 搜索按钮、添加按钮
    private JTable employeeTable; // 员工表格：用于显示员工信息的表格
    private DefaultTableModel tableModel; // 表格模型: 用于封装表格数据的对象
    private JPopupMenu rowMenu; // 行菜单

    private static ArrayList<Employee> employees = new ArrayList<>(); // 创建员工集合

    public EmployeeManagementFrame() {
        // 设置框架基本属性
        this.setTitle("员工信息管理界面");
        this.setSize(600, 400);
        this.setLocationRelativeTo(null); // 居中显示
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 调用设置面板的方法
        setupPanel();
    }

    // 设置面板的内容
    private void setupPanel() {
        // 传入当前对象
        EmployeeManagementFrame employeeManagement = this;
        // 设置布局
        setLayout(new BorderLayout()); // 设置框架布局为边界布局

        // 创建顶部面板（包含输入框、搜索按钮、添加按钮）
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10)); // 居中布局
        searchField = new JTextField(20); // 创建搜索框
        searchButton = new JButton("搜索"); // 创建搜索按钮
        addButton = new JButton("添加"); // 创建添加按钮

        // 为添加按钮添加事件监听
        addButton.addActionListener(e -> {
            //点击添加按钮后，打开添加员工界面
            new AddEmployeeFrame(employeeManagement).setVisible(true);
        });

        topPanel.add(searchField);
        topPanel.add(searchButton);
        topPanel.add(addButton);

        // 添加顶部面板
        add(topPanel, BorderLayout.NORTH);

        // 创建表格
        String[] columnNames = {"员工ID", "姓名", "性别", "年龄", "职位", "入职时间", "薪水", "部门", "电话"};
        tableModel = new DefaultTableModel(columnNames, 0); // 设置表格模型
        employeeTable = new JTable(tableModel);
        loadTableData();

        // 设置表格的右键菜单
        rowMenu = new JPopupMenu();
        JMenuItem modifyItem = new JMenuItem("修改");
        JMenuItem deleteItem = new JMenuItem("删除");
        rowMenu.add(modifyItem);
        rowMenu.add(deleteItem);

        // 右键点击表格行时显示菜单
        employeeTable.setComponentPopupMenu(rowMenu);
        employeeTable.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) { // 右键点击表格行时显示菜单
                if (e.isPopupTrigger()) {
                    int row = employeeTable.rowAtPoint(e.getPoint());
                    if (row >= 0 && row < employeeTable.getRowCount()) {
                        employeeTable.setRowSelectionInterval(row, row); // 设置选中的行
                    } else {
                        employeeTable.clearSelection();
                    }
                }
            }
        });

        // 添加表格滚动面板
        JScrollPane scrollPane = new JScrollPane(employeeTable);
        add(scrollPane, BorderLayout.CENTER);
    }

    // 加载表格数据
    private void loadTableData() {
        // 如果集合为空，则添加默认测试数据
        if (employees.isEmpty()) {
            employees.add(new Employee(1, "张三", "男", 30, "程序员", "2023-05-01", 8000, "IT", "12345678901"));
            employees.add(new Employee(2, "李四", "女", 28, "设计师", "2023-04-15", 6000, "设计", "12345678902"));
        }
        // 从集合中获取员工信息，并添加到表格中
        for (Employee employee : employees) {
            tableModel.addRow(new Object[]{
                    employee.getId(),
                    employee.getName(),
                    employee.getSex(),
                    employee.getAge(),
                    employee.getJob(),
                    employee.getHireDate(),
                    employee.getSalary(),
                    employee.getDepartment(),
                    employee.getPhone()
            });
        }
    }
    public static void main(String[] args) {
        EmployeeManagementFrame employeeManagementFrame = new EmployeeManagementFrame();
        employeeManagementFrame.setVisible(true);
    }

    public void addEmployee(Employee newEmployee) {
        // 将新员工信息添加到员工集合，并展示在表格中
        employees.add(newEmployee);
        tableModel.addRow(new Object[]{
                newEmployee.getId(),
                newEmployee.getName(),
                newEmployee.getSex(),
                newEmployee.getAge(),
                newEmployee.getJob(),
                newEmployee.getHireDate(),
                newEmployee.getSalary(),
                newEmployee.getDepartment(),
                newEmployee.getPhone()
        });
        System.out.println("添加员工成功！");
    }
}
